package com.hunliji.monitor.dao.utils;

import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class TemplateReplaceUtil {

    public static String replace(String template, Map<String, Object> paramMap){
        if (CollectionUtils.isEmpty(paramMap)) {
            return template;
        }
        AtomicReference<String> replaceResult = new AtomicReference<>(template);
        paramMap.forEach((key, value) -> {
            if (value == null) {
                value = "";
            }
            String tmpContent = replaceResult.get();
            if (tmpContent.contains(key)) {
                replaceResult.set(tmpContent.replaceAll( "\\{\\{" + key + "}}", value.toString()));
            }
        });
        return replaceResult.get();
    }
}
